<template>
  <div>
    <van-nav-bar
      title="地址编辑"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <div>
      <van-address-edit
        :area-list="areaList"
        show-search-result
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        tel-maxlength="11"
      />
    </div>
  </div>
</template>
<script>
import { Toast } from "vant";
import mySite from "@/common/commonData/index.js";
import { addressInsert } from "../common/Api/index.js";
export default {
  data() {
    return {
      areaList: mySite.areaList,
      searchResult: [],
      information: JSON.parse(sessionStorage.getItem("information")),
    };
  },

  methods: {
    onSave(item) {
      console.log(item);
      let arr = {
        username: item.name,
        phone: item.tel,
        address: `${item.province}/${item.city}/${item.county}`,
        detailed: item.addressDetail,
        uid: this.information.uid,
      };
      addressInsert(arr).then((res) => {
        if (res == "地址添加成功") {
          Toast.success("地址添加成功");
          console.log(res);
          if (this.$route.query.key) {
            // this.key = this.$route.query.key;
            this.$router.push({ path: "/addressList", query: { key: 1 } });
          } else {
            this.$router.push("/addressList");
          }
          // this.$router.push("/addressList");
        } else {
          Toast.fail(res);
        }
      });
    },
  },
};
</script>